<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:db="http://docbook.org/ns/docbook"
    xmlns:pbs="http://schema.pbs.gov.au" version="1.0">
    <xsl:output method="text"/>

    <xsl:param name="header" select="'no'"/>
    <xsl:param name="delimiter" select="','"/>
    <xsl:param name="eol" select="'&#xa;'"/>

    <xsl:key name="ident" match="*" use="@xml:id"/>

    <xsl:template match="/">
        <xsl:if test="$header = 'yes'">
            <xsl:text>Program Code</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Item Code</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Mnfr Code</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Dispensing Rule</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Reim. ex-man</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Reim. P2P</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Reim. Pack Price</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Reim. DPMQ</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Lowest ex-man</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Lowest P2P</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Lowest Pack Price</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Lowest DPMQ</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Mnfr ex-man</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Mnfr P2P</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Mnfr Pack Price</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Mnfr DPMQ</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>TGP</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>OSPC</xsl:text>
            <xsl:value-of select="$delimiter"/>
            <xsl:text>Brand Premium</xsl:text>
            <xsl:value-of select="$eol"/>
        </xsl:if>
        <xsl:apply-templates
            select="/pbs:root/pbs:schedule/pbs:listings-list/pbs:prescribing-rule[pbs:ready-prepared|pbs:infusible]"
        />
    </xsl:template>
    <xsl:template match="pbs:prescribing-rule">
        <xsl:apply-templates
            select="pbs:ready-prepared/pbs:pricing/pbs:prices/pbs:tpp-reference |
            pbs:infusible/pbs:pricing/pbs:reimbursement/pbs:prices/pbs:tpp-list/pbs:tpp-reference">
            <xsl:with-param name="item" select="."/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="pbs:tpp-reference">
        <xsl:param name="item" select="/.."/>

        <xsl:apply-templates
            select="$item/ancestor::pbs:listings-list/pbs:dispensing-rules-list/pbs:dispensing-rule">
            <xsl:with-param name="item" select="$item"/>
            <xsl:with-param name="tpp-ref" select="."/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="pbs:dispensing-rule">
        <xsl:param name="item" select="/.."/>
        <xsl:param name="tpp-ref" select="/.."/>

        <xsl:variable name="tpp" select="key('ident', substring($tpp-ref/@xlink:href, 2))"/>

        <xsl:variable name="mnfr-tpp-ref"
            select="$tpp-ref/ancestor::pbs:pricing/pbs:manufacturer/pbs:prices/pbs:tpp-list/pbs:tpp-reference[@xlink:href = $tpp-ref/@xlink:href]"/>

        <!--Program Code-->
        <xsl:value-of select="$item/../pbs:info/pbs:code"/>
        <xsl:value-of select="$delimiter"/>
        <!--Item Code-->
        <xsl:value-of select="$item/pbs:code"/>
        <xsl:value-of select="$delimiter"/>
        <!--Mnfr Code-->
        <xsl:value-of select="key('ident', substring($tpp/pbs:organisation-reference/@xlink:href, 2))/pbs:code"/>
        <xsl:value-of select="$delimiter"/>
        <!--Dispensing Rule-->
        <xsl:value-of select="@about"/>
        <xsl:value-of select="$delimiter"/>
        <!--Reim. ex-man-->
        <xsl:value-of select="$item/pbs:ready-prepared/pbs:pricing/pbs:reimbursement/pbs:ex-manufacturer |
            $tpp-ref/../pbs:tpuu-ex-manufacturer"/>
        <xsl:value-of select="$delimiter"/>
        <!--Reim. P2P-->
        <xsl:value-of select="$item/pbs:ready-prepared/pbs:pricing/pbs:reimbursement/pbs:to-pharmacist/pbs:price[@dispensing-rule = current()/@about]/pbs:amount |
            $tpp-ref/../pbs:tpuu-ex-manufacturer"/>
        <xsl:value-of select="$delimiter"/>
        <!--Reim. Pack Price-->
        <xsl:value-of select="$item/pbs:ready-prepared/pbs:pricing/pbs:reimbursement/pbs:pack-price/pbs:price[@dispensing-rule = current()/@about]/pbs:amount |
            $tpp-ref/../pbs:tpuu-pharmacy-price[@dispensing-rule = current()/@about]/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--Reim. DPMQ-->
        <xsl:value-of select="$item/*/pbs:pricing/pbs:reimbursement/pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--Lowest ex-man-->
        <xsl:value-of select="$delimiter"/>
        <!--Lowest P2P-->
        <xsl:value-of select="$item/pbs:ready-prepared/pbs:pricing/pbs:lowest/pbs:to-pharmacist/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--Lowest Pack Price-->
        <xsl:value-of select="$item/pbs:ready-prepared/pbs:pricing/pbs:lowest/pbs:pack-price/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--Lowest DPMQ-->
        <xsl:value-of select="$item/pbs:ready-prepared/pbs:pricing/pbs:lowest/pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--Mnfr ex-man-->
        <xsl:choose>
            <xsl:when test="$item/pbs:infusible">
                <xsl:value-of select="$tpp/pbs:tpuu-ex-manufacturer"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$tpp/pbs:ex-manufacturer"/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="$delimiter"/>
        <!--Mnfr P2P-->
        <xsl:choose>
            <xsl:when test="$item/pbs:infusible">
                <xsl:value-of select="$tpp/pbs:tpuu-ex-manufacturer"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$tpp-ref/../pbs:to-pharmacist/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="$delimiter"/>
        <!--Mnfr Pack Price-->
        <xsl:choose>
            <xsl:when test="$item/pbs:infusible">
                <xsl:value-of select="$mnfr-tpp-ref/../pbs:tpuu-pharmacy-price/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$tpp-ref/../pbs:pack-price/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="$delimiter"/>
        <!--Mnfr DPMQ-->
        <xsl:value-of select="$tpp-ref/../pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:amount |
            $item/pbs:infusible/pbs:pricing/pbs:manufacturer/pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--TGP-->
        <xsl:value-of select="$tpp-ref/../pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:contribution[pbs:type = 'contrib:therapeutic-group']/pbs:amount |
            $item/pbs:infusible/pbs:pricing/pbs:manufacturer/pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:contribution[pbs:type = 'contrib:therapeutic-group']/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--OSPC-->
        <xsl:value-of select="$tpp-ref/../pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:contribution[pbs:type = 'contrib:special-patient']/pbs:amount |
            $item/pbs:infusible/pbs:pricing/pbs:manufacturer/pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:contribution[pbs:type = 'contrib:special-patient']/pbs:amount"/>
        <xsl:value-of select="$delimiter"/>
        <!--Brand Premium-->
        <xsl:value-of select="$tpp-ref/../pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:contribution[pbs:type = 'contrib:brand-premium']/pbs:amount |
            $item/pbs:infusible/pbs:pricing/pbs:manufacturer/pbs:dpmq/pbs:price[@dispensing-rule = current()/@about]/pbs:contribution[pbs:type = 'contrib:brand-premium']/pbs:amount"/>
        
        <xsl:value-of select="$eol"/>
    </xsl:template>
</xsl:stylesheet>
